package org.langcom.change;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RefreshStore {

	private static RefreshStore instance = new RefreshStore();
	private List<RefreshManager> managerList = Collections.synchronizedList(new ArrayList<RefreshManager>());
	private Map<String,RefreshManager> managerMap = Collections.synchronizedMap(new HashMap<String,RefreshManager>());

	private RefreshStore() {
	}

	public synchronized void addManager(RefreshManager manager) {
		managerList.add(manager);
		managerMap.put(manager.getName(), manager);
	}
	
	public synchronized void removeManager(RefreshManager manager) {
		managerList.remove(manager);
	}
	
	public synchronized RefreshManager getManager(String name) {
		RefreshManager refreshManager = managerMap.get(name);
		if(refreshManager == null) {
			refreshManager = new RefreshManager(name);
			managerMap.put(name, refreshManager);
		}
		return refreshManager;
	}
	
	public synchronized void refresh(IRefreshable source,String ... names) {
		for (String name : names) {
			RefreshManager manager = getManager(name);
			if(manager != null) {
				manager.refresh(source);
			}
		}
	}
	
	public synchronized static RefreshStore getInstance() {
		return instance;
	}
}
